#include "TextField.h"
#include <QtGui/qfont.h>
#include <QtGui/qfontmetrics.h>
#include <QtGui/qpainter.h>

using namespace std;
using namespace GUI;

TextField::TextField(GraphDisplay& display, int zorder)
	:GraphObject(display, zorder),
	 mTextHeight(1.0),
	 mColor(RGBColor::NullColor),
	 mTextColor(RGBColor::Gray)
{
}

TextField::~TextField()
{
}

TextField& TextField::SetText(const string& text)
{
	mText = text;
	Change();
	return *this;
}

const string& TextField::Text() const
{
	return mText;
}

TextField& TextField::SetTextHeight(float h)
{
	mTextHeight = h;
	Change();
	return *this;
}

float TextField::TextHeight() const
{
	return mTextHeight;
}

TextField& TextField::SetTextColor(RGBColor c)
{
	mTextColor = c;
	Change();
	return *this;
}

RGBColor TextField::TextColor() const
{
	return mTextColor;
}

TextField& TextField::SetColor(RGBColor c)
{
	mColor = c;
	Change();
	return *this;
}

RGBColor TextField::Color() const
{
	return mColor;
}

void TextField::OnChange(DrawContext& dc)
{
	if(dc.handle.device == NULL) return;
	int width = static_cast<int>(dc.rect.right - dc.rect.left),
		height = static_cast<int>(dc.rect.bottom - dc.rect.top),
		hCenter = static_cast<int>((dc.rect.right + dc.rect.left)/2),
		vCenter = static_cast<int>((dc.rect.bottom + dc.rect.top)/2),
		fontSize = static_cast<int>(mTextHeight*height);
	QSize size(0, 0);
	if(fontSize > 0)
	{
		QFont font;
		font.fromString(QString("Arial"));
		font.setPixelSize(fontSize);
		font.setBold(true);
		QFontMetrics fm(font);
		QString text = QString::fromLocal8Bit(mText.c_str());
		text.append(" ").prepend(" ");
		size = fm.size(0, text);
	}

	switch(AspectRatioMode())
	{
	case AspectRatioMode::AdjustWidth:
		width = size.width();
		break;
	case AspectRatioMode::AdjustHeight:
		height = (height*width)/size.width();
		break;
	case AspectRatioMode::AdjustBoth:
		height = size.height();
		width = size.width();
		break;
	case AspectRatioMode::AdjustNone:
	default:
		; // NULL operation
	}
	dc.rect.left = hCenter - width/2;
	dc.rect.right = hCenter + width/2;
	dc.rect.top = vCenter - height/2;
	dc.rect.bottom = vCenter + height/2;
	GraphObject::OnChange(dc);
}

void TextField::OnPaint(const DrawContext& dc)
{
	DoPaint(dc, mTextColor, mColor);
}

void TextField::DoPaint(const DrawContext& dc, RGBColor textColor, RGBColor bkColor)
{
	QPainter* p = dc.handle.painter;
	QRect rect(
		static_cast<int>(dc.rect.left),
		static_cast<int>(dc.rect.top),
		static_cast<int>(dc.rect.right - dc.rect.left),
		static_cast<int>(dc.rect.bottom - dc.rect.top)
		);
	QBrush brush;
	brush.setStyle(Qt::SolidPattern);
	if(mColor != RGBColor::NullColor)
	{
		QColor bkColor(mColor.R(), mColor.G(), mColor.B());
		brush.setColor(bkColor);
		p->fillRect(rect, brush);
	}
	QFont font;
	font.fromString(QString("Arial"));
	font.setPixelSize(static_cast<int>(mTextHeight*(dc.rect.bottom-dc.rect.top)));
	font.setBold(true);
	QColor tColor(textColor.R(), textColor.G(), textColor.B());
	QPen pen;
	brush.setColor(tColor);
	pen.setColor(tColor);
	p->setPen(pen);
	p->setBrush(brush);
	p->setFont(font);

	QString text = QString::fromLocal8Bit(mText.c_str());
	text.append(" ").prepend(" ");
	p->drawText(rect, Qt::AlignCenter, text);
}